//
//  CGModifyPhoneViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/18.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGModifyPhoneViewController.h"

@interface CGModifyPhoneViewController ()<UIAlertViewDelegate>

@property(nonatomic,strong)CGRegistTextView *phoneText;            //手机号
@property(nonatomic,strong)CGRegistTextView *verifyText;           //验证码
@property(nonatomic,strong)JKCountDownButton *verifyButton;        //验证码
@property(nonatomic,strong)UIButton *saveButton;                   //保存

@property(nonatomic,strong)UIWindow *coverWindow;
@property(nonatomic,strong)UIView *shadeView;
@property(nonatomic,strong)UIActivityIndicatorView *activity;

@end

#define TEXT_MARGIN 10    //文本框的边距
@implementation CGModifyPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"修改手机号";
    
    [self layoutUI];
}

-(void)layoutUI{
    
    CGRect aFrame = CGRectMake(20, 10, SCREEN_WIDTH - 20 - 20, 44);
    //手机号
    self.phoneText = [[CGRegistTextView alloc]initWithFrame:aFrame andImageName:@"login_phone_icon"];
    self.phoneText.textField.placeholder = @"请输入新手机号";
    self.phoneText.textField.delegate = self;
    [self.view addSubview:self.phoneText];
    
    //验证码
    aFrame.origin.y = CGRectGetMaxY(self.phoneText.frame) + TEXT_MARGIN;
    aFrame.size.width = SCREEN_WIDTH - 20 - 110;
    self.verifyText = [[CGRegistTextView alloc]initWithFrame:aFrame andImageName:@"login_very_icon"];
    self.verifyText.textField.placeholder = @"请输入验证码";
    self.verifyText.textField.delegate = self;
    [self.view addSubview:self.verifyText];
    
    //获取验证码Button
    aFrame.origin.x = SCREEN_WIDTH - 100;
    aFrame.size.width = 80;
    self.verifyButton = [[JKCountDownButton alloc]initWithFrame:aFrame];
    [self.verifyButton setBackgroundImage:[UIImage imageNamed:@"login_very_btn_bg"] forState:UIControlStateNormal];
    [self.verifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.verifyButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.verifyButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    [self.view addSubview:self.verifyButton];
    [self.verifyButton addTarget:self action:@selector(getVerfyCode:) forControlEvents:UIControlEventTouchUpInside];
    
    //立即保存Button
    aFrame.origin.y = CGRectGetMaxY(self.verifyText.frame) + 20;
    aFrame.origin.x = 20;
    aFrame.size.width = SCREEN_WIDTH - 40;
    self.saveButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.saveButton setBackgroundImage:[UIImage imageNamed:@"login_btn_bg"] forState:UIControlStateNormal];
    [self.saveButton setTitle:@"立即保存" forState:UIControlStateNormal];
    self.saveButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:self.saveButton];
    //添加点击事件
    [self.saveButton addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark 获取验证码事件
-(void)getVerfyCode:(JKCountDownButton *)sender{
    
    //1.手机号有效性性验证，正则表达式
    if (![Tools isMobileNumber:self.phoneText.textField.text]) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请输入正确的手机号码"];
        return;
    }
    //2.发送请求获取验证码
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"tphone"] = self.phoneText.textField.text;
    
    ZYHRequest *request = [ZYHRequest new];
    NSData *responseData = [request getSyncEncryptRequest:@"/main/getCode.app?" parameters:parameters];
//    NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
//    NSLog(@"result:%@",result);
    //倒计时
    sender.enabled = NO;
    [sender startWithSecond:60];
    [sender didChange:^NSString *(JKCountDownButton *countDownButton, int second) {
        NSString *title = [NSString stringWithFormat:@"剩%d秒",second];
        return title;
    }];
    [sender didFinished:^NSString *(JKCountDownButton *countDownButton, int second) {
        countDownButton.enabled = YES;
        return @"重新获取";
    }];
    
}



-(void)saveButtonClick{
    
    //1.手机号有效性性验证，正则表达式
    if (![Tools isMobileNumber:self.phoneText.textField.text]) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请输入正确的手机号码"];
        return;
    }

    NSString *verify = [self.verifyText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (verify.length == 0) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请输入验证码"];
        return;
    }

    [self popShadeView];   //弹出延时框

    //2.发送请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"account"] = [CGAccountDefault shareAccount].account;
    parameters[@"password"] = [CGAccountDefault shareAccount].password;
    parameters[@"tphone"] = self.phoneText.textField.text;
    parameters[@"code"] = self.verifyText.textField.text;
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/main/updateTphone.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

        if ([result rangeOfString:@"account"].location != NSNotFound) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"恭 喜" message:@"手机号码修改成功!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertView show];
            
        }else{
            
            [Tools alertWithTitle:@"温馨提示" andMessage:@"手机号或验证码不匹配"];
         
        }
        
        [self hidShadeView];
        return ;
        
        
    } failure:^(NSError *error) {
        
        [self hidShadeView];
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        return;
    }];

}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];

    return YES;
}

#pragma mark 弹出网络延时加载框
-(void)popShadeView{
    
    self.shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.3;
    self.coverWindow = [UIApplication sharedApplication].keyWindow;
    [self.coverWindow addSubview:self.shadeView];
    
    //添加一个等待进度轮
    self.activity = [[UIActivityIndicatorView alloc]init];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    //self.activity.color = [UIColor blueColor];
    CGPoint center = self.view.center;
    self.activity.center = center;
    [self.activity startAnimating];
    [self.coverWindow addSubview:self.activity];
    
}

#pragma mark 取消网络延时加载框
-(void)hidShadeView{
    
    self.shadeView.hidden = YES;
    [self.activity stopAnimating];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
